package com.slg.android.manyanime;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.slg.android.manyanime.adapter.CategoryDetailAdapter;
import com.slg.android.manyanime.bean.ChannelInfo;
import com.slg.android.manyanime.utils.ApiUtils;
import com.slg.android.manyanime.utils.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class CategoryActivity extends FragmentActivity implements PullToRefreshBase.OnRefreshListener<ListView>, AdapterView.OnItemClickListener {
    @ViewInject(R.id.common_title_title_textView)
    private TextView title;
    @ViewInject(R.id.category_animation_info_list)
    private PullToRefreshListView pullToRefreshListView;
    @ViewInject(R.id.common_loading)
    private RelativeLayout loading;
    @ViewInject(R.id.common_loading_image)
    private ImageView loadingImage;
    private Animation animation;
    private String id;
    private List<ChannelInfo> data;
    private ListView listView;
    private int count;
    private int currentPage = 1;
    private CategoryDetailAdapter adapter;
    private CallBack callBack;
    private HttpUtils httpUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_category);
        ViewUtils.inject(this);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        if (!TextUtils.isEmpty(id)) {
            if (NetUtil.isConnectInternet(this)) {
                //初始化加载动画
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_common_rotate);
                loading.setVisibility(View.VISIBLE);
                loadingImage.startAnimation(animation);
                httpUtils = new HttpUtils();
                callBack = new CallBack(false);
                httpUtils.send(HttpRequest.HttpMethod.GET
                        , ApiUtils.CATEGORY_DETAIL + id + "&page=" + currentPage
                        , callBack);

            }
        } else {
            title.setText("类别");
        }
    }

    @OnClick({R.id.common_title_back_imageView})
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.common_title_back_imageView:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        if (refreshView.isRefreshing()) {
            if (currentPage * 15 >= count) {
                Toast.makeText(this, "没有更多数据了", Toast.LENGTH_SHORT).show();
            } else {
                currentPage++;
                callBack.loadMore = true;
                httpUtils.send(HttpRequest.HttpMethod.GET
                        , ApiUtils.CATEGORY_DETAIL + id + "&page=" + currentPage
                        , callBack);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int headerViewsCount = listView.getHeaderViewsCount();
        ChannelInfo channelInfo = data.get(position - headerViewsCount);
        Intent intent = new Intent(this, AnimationDetailActivity.class);
        intent.putExtra("id", channelInfo.getVideoId());
        startActivity(intent);
    }

    private class CallBack extends RequestCallBack<String> {
        private boolean loadMore;

        public CallBack(boolean loadMore) {
            this.loadMore = loadMore;
        }

        @Override
        public void onSuccess(ResponseInfo<String> stringResponseInfo) {
            String json = stringResponseInfo.result;
            try {
                JSONObject jsonObject = new JSONObject(json);
                count = Integer.parseInt(jsonObject.getString("count"));
                JSONObject object = jsonObject.getJSONObject("info");
                String name = object.getString("name");
                title.setText(name);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (loadMore) {//上拉加载
                data.addAll(ChannelInfo.parseData(json));
                adapter.notifyDataSetChanged();
                pullToRefreshListView.onRefreshComplete();
            } else {//初始化
                data = ChannelInfo.parseData(json);
                listView = pullToRefreshListView.getRefreshableView();
                listView.setDividerHeight(0);
                pullToRefreshListView.setOnRefreshListener(CategoryActivity.this);
                if (data != null) {
                    adapter = new CategoryDetailAdapter(CategoryActivity.this, data);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(CategoryActivity.this);
                    //结束加载动画
                    loading.setVisibility(View.GONE);
                    animation.cancel();
                }
            }
        }

        @Override
        public void onFailure(HttpException e, String s) {
            Toast.makeText(CategoryActivity.this, R.string.no_network, Toast.LENGTH_SHORT).show();
            if (loadMore) {
                pullToRefreshListView.onRefreshComplete();
            }
        }
    }


}
